import axios from "axios";
import { Message } from "element-ui";

const http = axios.create({
  baseURL: "http://localhost:8095",
  timeout: 6000,
});

// 提取请求拦截器
http.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers["Authorization"] = `Bearer ${token}`;
    }
    return config;
  },
  (error) => Promise.reject(error)
);

// 提取响应拦截器
http.interceptors.response.use(
  (response) => response.data,
  (error) => {
    Message.error(error.response?.data?.message || "网络繁忙，请稍后重试");
    return Promise.reject(error);
  }
);

export default http;
